package org.apache.hop.pipeline.transforms.script;

import java.util.ArrayList;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;

public class ShellScriptEngineFactory implements ScriptEngineFactory {

  @Override
  public String getEngineName() {
    return "cpython";
  }

  @Override
  public String getEngineVersion() {
    return "1.0";
  }

  @Override
  public List<String> getExtensions() {
    List<String> extensions = new ArrayList<>();
    extensions.add("py");
    return extensions;
  }

  @Override
  public List<String> getMimeTypes() {
    List<String> mimeTypes = new ArrayList<>();
    mimeTypes.add("application/x-sh");
    return mimeTypes;
  }

  @Override
  public List<String> getNames() {
    List<String> names = new ArrayList<>();
    names.add("cpython");
    return names;
  }

  @Override
  public String getLanguageName() {
    return "cpython";
  }

  @Override
  public String getLanguageVersion() {
    return "1.0";
  }

  @Override
  public Object getParameter(String key) {
    switch (key) {
      case ScriptEngine.ENGINE:
        return getEngineName();
      case ScriptEngine.ENGINE_VERSION:
        return getEngineVersion();
      case ScriptEngine.LANGUAGE:
        return getLanguageName();
      case ScriptEngine.LANGUAGE_VERSION:
        return getLanguageVersion();
      case ScriptEngine.NAME:
        return getNames();
      default:
        return null;
    }
  }

  @Override
  public String getMethodCallSyntax(String obj, String method, String... args) {
    return null;
  }

  @Override
  public String getOutputStatement(String toDisplay) {
    return "print( " + toDisplay + ")";
  }

  @Override
  public String getProgram(String... statements) {
    StringBuilder program = new StringBuilder();
    for (String statement : statements) {
      program.append(statement).append("\n");
    }
    return program.toString();
  }

  @Override
  public ScriptEngine getScriptEngine() {
    return new ShellScriptEngine();
  }
}
